<template>
	<!-- pages/loginRedirect.wxml -->
	<view class="loginRedirectPage">
		<view class="text">登录跳转中~</view>
	</view>
</template>

<script>
	// pages/loginRedirect.js
	const app = getApp();
	export default {
		data() {
			return {};
		}
		/**
		 * 生命周期函数--监听页面加载
		 */
		,
		onLoad: function(options) {
			for(let item in options){
				options[item] = options[item].replace('huoniaowh', '?').replace(/huoniaolj/g, '&').replace(/huoniaodh/g, '=');
			}
			uni.showLoading({
				title: '登陆跳转中~'
			});
			uni.removeStorageSync('logoutSelf'); //删除手动退出登路
			var that = this;
			var url = decodeURIComponent(options.url);
			var scene = parseInt(decodeURIComponent(options.scene));
			var userid = options.userid;
			if (options.path && options.path != 'null' && options.path != 'undefined') {
				var path = decodeURIComponent(options.path);
			} else {
				var path = '';
			}
			var access_token = options.access_token;
			var refresh_token = options.refresh_token;
			if (userid) {
				uni.setStorage({
					key: 'userid',
					data: userid
				});
				app.globalData.userid = userid;
			}
			if (access_token) {
				uni.setStorage({
					key: 'access_token',
					data: access_token
				});
				app.globalData.access_token = access_token;
			}
			if (refresh_token) {
				uni.setStorage({
					key: 'refresh_token',
					data: refresh_token
				});
				app.globalData.refresh_token = refresh_token;
				var token_expired = Date.parse(new Date()) / 1000 + 7200 - 20; //两个小时有效期，保险起见，用1小时59分钟40秒
				uni.setStorage({
					key: 'token_expired',
					data: token_expired
				});
				app.globalData.token_expired = token_expired;
			}
			if (path) {
				//跳转原声页面路径
				//后退
				if (path == '/pages/goback') {
					var referpage = getCurrentPages()[0].__displayReporter.showReferpagepath; //上个页面
					//前面有页面时
					if (referpage) {
						uni.navigateBack({
							delta: 1
						});
					} else {
						uni.redirectTo({
							url: '/pages/index/index'
						});
					}
				} else {
					uni.redirectTo({
						url: path
					});
				}

				return false;
			}
			if (url != 'undefined') {
				var urlParam = url.split('?')[1];
				userid = app.getUrlPara(urlParam, 'uid');
				access_token = app.getUrlPara(urlParam, 'access_token');
				refresh_token = app.getUrlPara(urlParam, 'refresh_token');
				uni.setStorage({
					key: 'userid',
					data: userid
				});
				uni.setStorage({
					key: 'access_token',
					data: access_token
				});
				uni.setStorage({
					key: 'refresh_token',
					data: refresh_token
				});
				var token_expired = Date.parse(new Date()) / 1000 + 7200 - 20; //两个小时有效期，保险起见，用1小时59分钟40秒
				uni.setStorage({
					key: 'token_expired',
					data: token_expired
				});
				app.globalData.userid = userid;
				app.globalData.access_token = access_token;
				app.globalData.refresh_token = refresh_token;
				app.globalData.token_expired = token_expired;
				uni.navigateBack({
					delta: 2,
					success: function(e) {
						//后退到上一页并重新打开登录前的页面
						uni.reLaunch({
							url: '/pages/redirect/index?url=' + encodeURIComponent(url)
						});
					},
					fail: function() {
						//没有上一页，在当前页直接打开
						uni.reLaunch({
							url: '/pages/redirect/index?url=' + encodeURIComponent(url)
						});
					}
				});
			}
		},
		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {},
		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {},
		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {},
		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {},
		methods: {
			getQueryVariable: function(variable, url) {
				var query = url.split('?')[1];
				var vars = query.split('&');
				for (var i = 0; i < vars.length; i++) {
					var pair = vars[i].split('=');
					if (pair[0] == variable) {
						return pair[1];
					}
				}
				return false;
			}
		}
	};
</script>
<style>
	@import './index.css';
</style>